const regeneratorRuntime = require('../../../utils/runtime.js')
const $ = require('../../../utils/util.js')
const app = getApp()
var Store = app.Store

Page({
  data: {
    company_id: 0, // 公司id
    lockCabinetList: [], // 某个公司下的电表箱列表
    isShowAdd: false, // 是否显示添加公司
    isKong: !1, // 是否是空
  },
  onLoad: function (options) {
    this.setData({
      company_id: options.company_id || 0
    })
    this._lockCabinet()
  },
  onShow: function (options) {

    this._lockCabinet()
  },
  _lockCabinet() { // 获取某个公司下的电表箱
    return new Promise((resolve, reject) => {
      app.$http.post('/lock/lockCabinet', {
        company_id: this.data.company_id
      }).then(res => {
        if (res && res.code == 0) {
          this.setData({
            lockCabinetList: res.data
          })

          if (this.data.lockCabinetList.length == 0) {
            this.setData({
              isKong: !0
            })
          }

          resolve(res)
        }

      })
    })
  },
  _lockCabinetAdd() { // 添加某个公司下的电表箱
    if ($.isNull(this.data.name)) {
      $.toast('请输入电表柜名称')
      this.setData({
        isShowAdd: !0
      })
      return
    } else {
      return new Promise((resolve, reject) => {
        app.$http.post('/lock/lockCabinetAdd', {
          company_id: this.data.company_id,
          name: this.data.name,
        }).then(res => {
          if (res && res.code == 0) {
            $.toast('添加成功')
            this._lockCabinet()
            this.setData({
              name: '',
              isShowAdd: false
            })
          }
          resolve(res)
        })
      })
    }
  },
  lockCabinetAddHandle() { // 添加公司
    // this.setData({
    //   isShowAdd: truecompany_id=
    // })
    $.linkurl('/pages/lockMange/addCabinet/addCabinet?company_id='+this.data.company_id)
  },

  //input 输入
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.name]: e.detail.value
    })
  },
  onCloseAdd() { // 关闭弹框
    this.setData({
      isShowAdd: false
    })
  },
  toNextHandle(e) { // 去下个页面 
    let { item: { name: cabinet_name, id: cabinet_id } } = $.detail(e)
    console.log(cabinet_name, cabinet_id)

    Store.dispatch({
      type: 'UPDATE_IS_FROM_CHOOSECABINET',
      isFromChooseCabinet: !!1
    })

    Store.dispatch({
      type: 'UPDATE_CABINET_NAME',
      cabinet_name
    })

    Store.dispatch({
      type: 'UPDATE_CABINET_ID',
      cabinet_id
    })

    setTimeout(()=>{
      $.linkurl('', 'back')
    },500)
    
  }
})